package com.sets.speedtest.activity;
import swipebacklayout.SwipeBackLayout;
import swipebacklayout.app.SwipeBackActivityBase;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import com.sets.speedtest.R;
import com.sets.speedtest.common.CommDialog;
import com.sets.speedtest.common.CommToast;
import com.sets.speedtest.listener.CommListener;
import com.wangjie.androidinject.annotation.present.AIActivity;
/**
* @author : lipan
* @create_time : 2014年8月15日 下午3:24:44
* @desc : Activity基类
* @update_person:
* @update_time :
* @update_desc :
*
*/
public class BaseActivity extends AIActivity implements SwipeBackActivityBase
{
private CommListener listener;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
protected void onStart()
{
super.onStart();
// 给每个view添加点击事件...
View needHideKeyBord = findViewById(R.id.main_parent);
if (null != needHideKeyBord)
{
setupUI(needHideKeyBord);
}
}
@Override
protected void onDestroy()
{
super.onDestroy();
}
/**
* 按钮点击事件
*
* @param v
*/
public void BtnClick(View v)
{
}
/** 通过Class跳转界面 **/
protected void startActivity(Class<?> cls)
{
startActivity(cls, null);
}
/** 含有Bundle通过Class跳转界面 **/
protected void startActivity(Class<?> cls, Bundle bundle)
{
Intent intent = new Intent();
intent.setClass(this, cls);
if (bundle != null)
{
intent.putExtras(bundle);
}
if (intent.resolveActivity(getPackageManager()) != null)
{
startActivity(intent);
} else
{
showLogError(this, "there is no activity can handle this intent: "
+ intent.getAction().toString());
}
}
/** 通过Action跳转界面 **/
protected void startActivity(String action)
{
Intent intent = new Intent();
intent.setAction(action);
if (intent.resolveActivity(getPackageManager()) != null)
{
startActivity(intent);
} else
{
showLogError(this, "there is no activity can handle this intent: "
+ intent.getAction().toString());
}
}
/** 含有Data通过Action跳转界面 **/
protected void startActivity(String action, Uri data)
{
Intent intent = new Intent();
intent.setAction(action);
intent.setData(data);
if (intent.resolveActivity(getPackageManager()) != null)
{
startActivity(intent);
} else
{
showLogError(this, "there is no activity can handle this intent: "
+ intent.getAction().toString());
}
}
/**
* 获得Parcelable中的参数
*
* @return
*/
protected Object getParcel(String key)
{
return getIntent().getParcelableExtra(key);
}
/**
* 获得Parcelable中的参数
*
* @return
*/
protected Object getParcelArrayList(String key)
{
return getIntent().getParcelableArrayListExtra(key);
}
/**
* 获得Activity
*
* @return
*/
protected Activity getActivity()
{
return (Activity) this;
}
/**
* 获得上下文对象
*
* @return
*/
// protected Context getContext()
// {
// return this;
// }
/** 结束Activity **/
protected void finishActivity()
{
super.finish();
}
/** Debug输出Log日志 **/
protected void showLogInfo(Activity activity, String msg)
{
String className = activity.getClass().getSimpleName();
Log.i(className, msg);
}
/** Debug输出Log日志 **/
protected void showLogDebug(Activity activity, String msg)
{
String className = activity.getClass().getSimpleName();
Log.d(className, msg);
}
/** Error输出Log日志 **/
protected void showLogError(Activity activity, String msg)
{
String className = activity.getClass().getSimpleName();
Log.e(className, msg);
}
/** 保持屏幕常亮 **/
protected void keepScreenOn()
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
/** 取消屏幕常亮 **/
protected void keepWake()
{
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
/** 启动服务 **/
protected void startService(Class<?> cls)
{
startService(cls, null);
}
/** 启动服务 **/
protected void startService(Class<?> cls, Bundle bundle)
{
Intent intent = new Intent(this, cls);
if (bundle != null)
{
intent.putExtras(bundle);
}
startService(intent);
}
/** 使用Dialog进行调试 **/
protected void debugWithDialog(String s)
{
CommDialog.showInfoDialog(getContext(), s, null);
}
/** 使用toast进行调试 **/
protected void debugWithToast(String s)
{
CommToast.showInfo(getContext(), s);
}
@Override
public SwipeBackLayout getSwipeBackLayout() {
return getSwipeBackLayout();
}
@Override
public void setSwipeBackEnable(boolean enable) {
getSwipeBackLayout().setEnableGesture(enable);
}
@Override
public void scrollToFinishActivity() {
getSwipeBackLayout().scrollToFinishActivity();
}
public void setListener(CommListener listener)
{
this.listener = listener;
}
/**
* 给每个非输入框的view添加touch事件
*
* @param view
*/
private void setupUI(View view)
{
Log.i("View.Tag", view.getTag()+"");
if (!(view instanceof EditText))
{
view.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (null!=listener)listener.allViewsOnTouchExcludeInput();
return false;
}
});
}
if (view instanceof ViewGroup)
{
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
{
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
}